package org.baitiaojun.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.baitiaojun.dao.SetMealCheckGroupMapper;
import org.baitiaojun.dao.SetMealMapper;
import org.baitiaojun.pojo.SetMeal;
import org.baitiaojun.pojo.SetMealCheckGroupKey;
import org.baitiaojun.service.SetMealService;
import org.baitiaojun.util.FileNameUtil;
import org.baitiaojun.util.MessageConstant;
import org.baitiaojun.vo.PageResult;
import org.baitiaojun.vo.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * @类名 SetMealServiceImpl
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2022/5/26 13:20
 * @版本 1.0
 */
@Service(interfaceClass = SetMealService.class)
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetMealMapper setMealMapper;

    @Autowired
    private SetMealCheckGroupMapper setMealCheckGroupMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void insertSetMeal(SetMeal setMeal, Integer... checkgroupIds) throws Exception {
        int setMealRes = setMealMapper.insert(setMeal);
        if (setMealRes == 0) {
            throw new Exception(MessageConstant.ADD_SETMEAL_FAIL);
        }

        if (checkgroupIds.length > 0) {
            SetMealCheckGroupKey setMealCheckGroupKey = new SetMealCheckGroupKey();
            setMealCheckGroupKey.setSetmealId(setMeal.getId());
            for (Integer checkgroupId:checkgroupIds) {
                setMealCheckGroupKey.setCheckgroupId(checkgroupId);
                int setMealCheckGroupRes = setMealCheckGroupMapper.insert(setMealCheckGroupKey);
                if (setMealCheckGroupRes == 0) {
                    throw new Exception(MessageConstant.ADD_SETMEAL_FAIL);
                }
            }
        }
    }

    @Override
    public PageResult<SetMeal> splitPageByConditions(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String conditions = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage, pageSize);

        PageResult<SetMeal> pageResult = null;
        List<SetMeal> setMealList =  setMealMapper.selectByConditions(conditions);
        if (setMealList != null) {
            pageResult = new PageResult<>();
            pageResult.setRows(setMealList);
            pageResult.setTotal(new PageInfo<>(setMealList).getTotal());
        }
        return pageResult;
    }

    @Override
    public SetMeal querySetMealById(Integer id) {
        return setMealMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void editSetMeal(Integer[] checkgroupIds, SetMeal setMeal) throws Exception {
        Integer id = setMeal.getId();
        List<Integer> ids = setMealCheckGroupMapper.selectCheckGroupIdsBySetMealId(id);
        if (ids.size() > 0) {
            int res = setMealCheckGroupMapper.deleteSetMealById(setMeal.getId());
            if (res == 0) {
                throw new Exception(MessageConstant.DELETE_CHECKGROUP_SETMEAL_FAIL);
            }
        }

        if (checkgroupIds.length > 0) {
            SetMealCheckGroupKey setMealCheckGroupKey = new SetMealCheckGroupKey();
            setMealCheckGroupKey.setSetmealId(setMeal.getId());
            for (Integer checkgroupId:checkgroupIds) {
                setMealCheckGroupKey.setCheckgroupId(checkgroupId);
                int setMealCheckGroupRes = setMealCheckGroupMapper.insert(setMealCheckGroupKey);
                if (setMealCheckGroupRes == 0) {
                    throw new Exception(MessageConstant.ADD_CHECKGROUP_SETMEAL_FAIL);
                }
            }
        }

        int setMealRes = setMealMapper.updateByPrimaryKey(setMeal);
        if (setMealRes == 0){
            throw new Exception(MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void removeSetMeal(Integer id) throws Exception {
        List<Integer> ids = setMealCheckGroupMapper.selectCheckGroupIdsBySetMealId(id);

        if (!ObjectUtils.isEmpty(ids)) {
            int setMealCheckGroupRes = setMealCheckGroupMapper.deleteSetMealById(id);
            if (setMealCheckGroupRes == 0) {
                throw new Exception(MessageConstant.DELETE_CHECKGROUP_SETMEAL_FAIL);
            }
        }

        int setMealRes = setMealMapper.deleteByPrimaryKey(id);
        if (setMealRes == 0) {
            throw new Exception(MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }
}
